/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package group2assignone.view.manager;

import group2assignone.model.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 *
 * @author Natalie
 */
public class AirportTableView extends JPanel
{

    private JList apList = new JList();
    //private Airport model;
    private AirportTable table;
    private JTable apTable;
    private JScrollPane tableScroll = new JScrollPane();
    private int selectedRow;

    /**
     * sets the manager table
     * @param managerList
     */
    public AirportTableView(ArrayList<Airport> airportList) {
        table = new AirportTable(airportList);
        addComponents();
    }

    //@Override
    public void addComponents() {
        //setTitle("Airport List");

        apTable = new JTable(table);
        apTable.setRowSelectionAllowed(true);
        apTable.setColumnSelectionAllowed(false);
        apTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tableScroll = (new JScrollPane(apTable));
        add(tableScroll);

        apTable.getSelectionModel().addListSelectionListener(new RowListener());
        selectedRow = apTable.getSelectedRow();
    }

    /**
     * This function returns a boolean result if there is a selected manager
     * @return
     * return true if is a selection
     * return false if there is no selection
     */
    public boolean isNoSelection() {
        return apList.isSelectionEmpty();
    }

    /**
     * this function returns the selected row number
     * this is to enable deleting and editing of the correct table row
     * @return
     */
    public String[] getRow() {
        String[] row = new String[apTable.getColumnCount()];
        int column = 0;

        for (String s : row) {
            System.out.println("col = " + column);
            // s.equals( table.getValueAt(selectedRow, column));
            row[column] = table.getValueAt(selectedRow, column);
            column++;
        }
        return row;

    }

    /**
     * This function returns the string entered into the textfield Username
     * @return
     */
    public String getAirportLoc() {
        int column = 0;
        String airportLoc = table.getValueAt(selectedRow, column);
        return airportLoc;
    }

    /**
     * This function is to set the selected row in program
     * @param row
     */
    public void setSelectedRow(int row) {
        selectedRow = row;

    }

    /**
     * this function is to return the private variable which holds the selected row
     * @return
     */
    public int getSelectedRow() {
        return selectedRow;
    }

    private class RowListener implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent arg0) {

            setSelectedRow(apTable.getSelectedRow());
        }
    }
}
